package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.EquipmentLineDao;
import com.sls.nzdapplication.model.EquipmentLine;
import com.sls.nzdapplication.app.MySharedPreferences;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EquipmentLineUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private EquipmentLine equipmentLine;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public EquipmentLineUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertEquipmentLine(EquipmentLine equipmentLine) {
        equipmentLine.setCreateBy(username);
        equipmentLine.setCreateTime(new Date());
        manager.getDaoSession().insertOrReplace(equipmentLine);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultEquipmentLine(final List<EquipmentLine> equipmentLines) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (EquipmentLine equipmentLine : equipmentLines) {
                    manager.getDaoSession().insertOrReplace(equipmentLine);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultEquipmentLine2(final List<EquipmentLine> equipmentLines) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (EquipmentLine equipmentLine : equipmentLines) {
                    manager.getDaoSession().insertOrReplace(equipmentLine);
                }
            }
        });
    }

    /**
     * 删除数据
     */
    public void deleteEquipmentLine(EquipmentLine equipmentLine) {
        manager.getDaoSession().delete(equipmentLine);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateEquipmentLine(EquipmentLine equipmentLine) {
        equipmentLine.setModifyBy(username);
        equipmentLine.setModifyTime(new Date());
        manager.getDaoSession().update(equipmentLine);
    }

    /**
     * 按照主键返回单条数据
     */
    public EquipmentLine listOneEquipmentLine(String key) {
        return manager.getDaoSession().load(EquipmentLine.class, key);
    }

    /**
     * 根据线路ID查询设备线路信息。
     *
     * @param id 线路的唯一标识符。
     * @return 返回匹配该线路ID的所有设备线路对象列表。
     */
    public List<EquipmentLine> queryEquipmentLineByLineId(String id){
        // 创建查询构建器用于查询EquipmentLine类的实例
        QueryBuilder<EquipmentLine> builder = manager.getDaoSession().queryBuilder(EquipmentLine.class);

        // 执行查询，返回所有线ID与传入ID相匹配的设备线路列表
        List<EquipmentLine> equipmentLineList = builder.where(EquipmentLineDao.Properties.LineId.eq(id)).list();

        return equipmentLineList;
    }



    public List<EquipmentLine> queryEquipmentLineByStartAndEnd(String sid, String eid){
        QueryBuilder<EquipmentLine> builder = manager.getDaoSession().queryBuilder(EquipmentLine.class);
        List<EquipmentLine> equipmentLineList = builder.where(builder.and(EquipmentLineDao.Properties.StartEquipmentId.eq(sid),EquipmentLineDao.Properties.EndEquipmentId.eq(eid))).list();
        return  equipmentLineList;
    }

    public List<EquipmentLine> queryEquipmentLineByEid(String eid){
        QueryBuilder<EquipmentLine> builder = manager.getDaoSession().queryBuilder(EquipmentLine.class);
        List<EquipmentLine> equipmentLineList = builder.where(builder.or(EquipmentLineDao.Properties.StartEquipmentId.eq(eid),EquipmentLineDao.Properties.EndEquipmentId.eq(eid))).list();
        return  equipmentLineList;
    }



    /**
     * 查询全部数据
     */
    public List<EquipmentLine> queryAll() {
        return manager.getDaoSession().loadAll(EquipmentLine.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<EquipmentLine> equipmentLines = queryAll();
        for(EquipmentLine equipmentLine :equipmentLines){
            jsonArray.add(equipmentLine.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<EquipmentLine> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new EquipmentLine(jsonArray.getJSONObject(i)));
        }
        insertMultEquipmentLine2(list);
    }
}
